package main.gui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import main.Core;

import character.sprite.gui.CharacterSpriteBuilder;

public class Workspace implements ActionListener, WindowListener {

	private static final long serialVersionUID = -4569447018025061640L;

	private static Workspace instance = null;

	private JFrame frame;
	private JButton sprite_builder_button;

	/**
	 * Default constructor.
	 */
	private Workspace() {
		// Set look and feel to default system look and feel
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Singleton accessor.
	 */
	public static Workspace getInstance() {
		if (instance == null) {
			instance = new Workspace();
		}
		return instance;
	}

	/**
	 * Sets up and displays the frame.
	 */
	public void display() {
		// Set title
		frame = new JFrame("RPG Architect");
		// Set workspace frame properties
		frame.setSize(1024, 768);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setIconImage(new ImageIcon("SwordShieldIcon.png").getImage());
		// Setup GUI elements
		JButton spriteBuildingButton = new JButton("Sprite Builder");
		spriteBuildingButton.addActionListener(this);
		frame.setLayout(new GridLayout(2, 1));
		frame.add(spriteBuildingButton);
		frame.addWindowListener(this);
		// Set visible
		frame.setVisible(true);
	}

	/* Action Listener Methods */

	@Override
	public void actionPerformed(ActionEvent arg0) {
		JButton source = (JButton) arg0.getSource();
		if (source.equals(sprite_builder_button)) {
			// Character sprite edit button pressed
			CharacterSpriteBuilder builder = new CharacterSpriteBuilder(frame,
					null);
			builder.setVisible(true);
		}
	}

	/* Window Listener Methods */

	@Override
	public void windowClosing(WindowEvent arg0) {
		// Shutdown core
		Core.getInstance().shutdown();

		// Dispose frame
		frame.dispose();
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
	}

}
